<?php
/**
 * 个人中心 - JBlog
 * 
 * @copyright (c) 2008-2010 JBlog (www.lisijie.org)
 * @author lisijie <lisijie86@gmail.com>
 * @version $Id$
*/

!defined('IN_ADMIN') && exit('Access Denied!');

empty($ac) && $ac = 'main';

//个人信息
if ( $ac == 'main' ) {
	
	$userinfo = get_user($_USER['id'],'id');

//资料修改
} elseif ( $ac == 'profile' ) {
	
	if ( check_submit('dosubmit') ) {

		initGP(array('password', 'newpassword', 'newpassword2', 'sex', 'url'), 'P');
		$data = array();

		if ( empty($password) ) {
			prompt(__('请输入你的密码。'));
		}
		if ( md5($password) != $_USER['password'] ) {
			prompt(__('您输入的密码有误。'));
		}
		if ( !empty($newpassword) ) {
			if ( strlen($newpassword) < 6 ) {
				prompt(__('密码长度不能小于6位。'));
			} elseif ( $newpassword != $newpassword2 ) {
				prompt(__('两次输入的密码不一致。'));
			} else {
				$data['password'] = md5($newpassword);
			}
		}
		if ( in_array($sex, array(0,1,2)) ) $data['sex'] = $sex;
		if ( preg_match('/^http:\/\/[0-9a-z\-\.]{4,50}\.[a-z]{2,5}$/i', $url)) $data['url'] = $url;
		update_user($_USER['id'], $data);
		prompt(__('个人资料修改成功。'));
		
	} else {

		$userinfo = get_user($_USER['id'],'id');

	}

//我的评论
} elseif ( $ac == 'comment' ) {

	$page = max(1, getGP('page','G','int'));
	$pagesize = 10;
	$offset = ($page-1) * $pagesize;
	$url = '?mod=ucenter&ac=comment';
	$wheresql = " c.userid = '".$_USER['id']."' ";
	$count = $db->count('comment', "userid = '$_USER[id]'");
	$result = array();
	$query = $db->query("SELECT c.*, p.title,p.dateline AS posttime FROM ".tname('comment')." c JOIN ".tname('post')." p ON c.postid = p.id 
	WHERE $wheresql ORDER BY c.id DESC LIMIT $offset, $pagesize");
	while ( $row = $db->fetch_array($query) ) {
		$row['content'] = apply_filter('comment_content', $row['content']);
		$result[] = $row;
	}

//提到我的评论
} elseif ( $ac == 'atme' ) {
	
	$page = max(1, getGP('page','G','int'));
	$pagesize = 10;
	$offset = ($page-1) * $pagesize;
	$url = '?mod=ucenter&ac=atme';
	$wheresql = " c.atuids LIKE '%,".$_USER['id'].",%' ";
	$count = $db->count('comment c', $wheresql);
	$result = array();
	$query = $db->query("SELECT c.*, p.title,p.dateline AS posttime FROM ".tname('comment')." c JOIN ".tname('post')." p ON c.postid = p.id 
	WHERE $wheresql ORDER BY c.id DESC LIMIT $offset, $pagesize");
	while ( $row = $db->fetch_array($query) ) {
		$row['content'] = apply_filter('comment_content', $row['content']);
		$result[] = $row;
	}	
	
}

include admin_tpl('ucenter');;
?>